class Utils {
    now() {
        return Math.floor(Date.now() / 1000);
    }

    jumpPage(url, params, events, type) {
        // console.error("==>>")
        uni.$u.route({
            url: url,
            params: params,
            events: events,
            type: type || "to"
        })
    }

    back() {
        uni.$u.route({
            type: "back"
        })
    }
    showHHMMSS(time, showLen = -1) {
        let h = Math.floor(time / 3600);
        time = time % 3600;
        let m = Math.floor(time / 60);
        time = time % 60;
        let s = Math.floor(time);

        let arr = [];
        if (h > 0) {
            arr.push((h < 10 ? "0" + h : h) + "小时");
        }
        arr.push((m < 10 ? "0" + m : m) + "分");
        arr.push((s < 10 ? "0" + s : s) + "秒");
        if (showLen > 0) {
            arr.length = Math.min(arr.length, showLen);
        }
        return arr.join("");
    }
	getDate(){
		let time = new Date();
		return time.getFullYear()+'-'+time.getMonth()+'-'+time.getDate();
	}
	loadImg(src){
		return new Promise((resolve,reject)=>{
			uni.getImageInfo({
				src: src,
				complete:res=>{
					resolve(res);
				}
			});
		})
	}
	async preloadImg(list=[],callback=()=>{}){
		let length  = list.length;
		for(let i=0;i<list.length;i++){
			let res = await this.loadImg(list[i]);
			let percent = Math.round(100*(i+1)/length);
			callback(percent);
		}
	}
	getStep() {
		var that = this;
		wx.login({
			success: function(res) {
				var appid = "";
				var secret = "";
				if (res.code) {
					wx.request({
						url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid +
							'&secret=' + secret + '&js_code=' + res.code +
							'&grant_type=authorization_code',
						header: {
							'content-type': 'json'
						},
						success: function(res) {
							var session_key = res.data.session_key;
							that.getStepInfo(appid, session_key);
						}
					})
				}
			}
		})
	}
	//获取encryptedData（没有解密的步数）和iv（加密算法的初始向量）
	getStepInfo(appid, session_key) {
		wx.getSetting({
			success: function(res) {
				// console.log('getSetting:',res);
				// 未开启微信运动授权
				if (!res.authSetting['scope.werun']) {
					wx.showModal({
						title: '提示',
						content: '获取微信运动步数，需要开启计步权限',
						success: function(res) {
							if (res.confirm) {
								//跳转去设置
								wx.openSetting({
									success: function(res) {}
								})
							} else {
								//不设置
							}
						}
					})
				} else {
					wx.getWeRunData({
						success: function(res) {
							// console.log(res);
							// console.log("appid:" + appid + "session_key:" + session_key + "encryptedData:" + res.encryptedData + "iv:" + res.iv);
							// var encryptedData = res.encryptedData;
							// var iv = res.iv;
							// var pc = new WXBizDataCrypt(appid, session_key);
							// var data = pc.decryptData(encryptedData, iv)
							console.log(res)
						},
						fail: function(res) {
							wx.showModal({
								title: '提示',
								content: '开发者未开通微信运动，请关注“微信运动”公众号后重试',
								showCancel: false,
								confirmText: '知道了'
							})
						}
					})
				}
			}
		})
	}
	getWxStep(){
		wx.getWeRunData({
			success: function(res) {
				// console.log(res);
				// console.log("appid:" + appid + "session_key:" + session_key + "encryptedData:" + res.encryptedData + "iv:" + res.iv);
				// var encryptedData = res.encryptedData;
				// var iv = res.iv;
				// var pc = new WXBizDataCrypt(appid, session_key);
				// var data = pc.decryptData(encryptedData, iv)
				console.log(res)
			},
			fail: function(res) {
				wx.showModal({
					title: '提示',
					content: '开发者未开通微信运动，请关注“微信运动”公众号后重试',
					showCancel: false,
					confirmText: '知道了'
				})
			}
		})
	}
}
const utils = new Utils();
export default utils;
/**********************************************/
wx['utils'] = utils;
